#ifndef _CMD2_H
#define _CMD2_H

#include <GL/gl.h>
#include "textura.h"

#define NUM_FRAMES 97

/*---------------------------------
 	MD2 Header struct
---------------------------------*/
struct SCabeceraMD2
{
   int m_iNumMagico; 
   int m_iVersion;  
   int m_iAnchoSkin;  
   int m_iAltoSkin; 
   int m_iTamFrame; 
   int m_iNumSkins; 
   int m_iNumVertices; 
   int m_iNumTexCoords; 
   int m_iNumTriangulos; 
   int m_iNumGLCommands; 
   int m_iNumFrames; 
   int m_iOffsetSkins; 
   int m_iOffsetTexCoords; 
   int m_iOffsetTriangles; 
   int m_iOffsetFrames; 
   int m_iOffsetGlCommands; 
   int m_iFileSize; 
};

/*---------------------------------
	Vertex structure for MD2
---------------------------------*/
struct SMD2Vert
{
	float m_fVert[3];
	unsigned char m_ucReserved;
	float m_fNormal[3];
};


/*---------------------------------
	Frame information
---------------------------------*/
struct SMD2Frame
{
	float m_fScale[3];
	float m_fTrans[3];
	char m_caName[16];
	SMD2Vert * m_pVerts;

	//Cleans up after itself
	SMD2Frame()
	{
		m_pVerts = 0;
	}

	~SMD2Frame()
	{
		if(m_pVerts)
			delete [] m_pVerts;
	}
};

/*---------------------------------
	Triangle information
---------------------------------*/
struct SMD2Tri
{
	unsigned short m_sVertIndices[3];
	unsigned short m_sTexIndices[3];
};

/*---------------------------------
	Texture coords
---------------------------------*/
struct SMD2TexCoord{

	float m_fTex[2];
};


/*------------------------------------------------------------------------------
	Implements MD2 loader
------------------------------------------------------------------------*/
class CMd2
{
	
	CTextura  		m_Skin;
	SCabeceraMD2 	m_Cabecera;
	SMD2Frame 		* m_pFrames;	
	SMD2Tri 		* m_pTriangles;	
	SMD2TexCoord 	* m_pTexCoords;
	SMD2Vert 		* m_pVerts;
	
	Bool			m_bDelineado;
	float			m_ColorLinea[3];
	float			m_Normales[2048][3];
	GLuint 			m_Textura1D;
	float			m_fAnguloLuz[3];
	float			m_AnchoLinea;	
	float			m_fmax_X;
	float			m_fmax_Y;
	float			m_fmin_X;
	float			m_fmin_Y;	
	
public:

	CMd2();
	CMd2(const char * szFile);
	~CMd2();
	bool CargarSkin(const char * szFilename);
	bool Cargar(const char * szFilename,const char szDirBase[255]=PATH_MD2);
	void Liberar();
	void Dibujar();
	void Dibujar(unsigned int uiFrame=0);
	void DibujarCelShading(unsigned int uiFrame=0);

	/*---------------------------------
	 set Functions
	---------------------------------*/
	void setDelineado(Bool valor){m_bDelineado=valor;}	
	
	/*---------------------------------
	 get Functions
	---------------------------------*/
	float getMaxX(){return m_fmax_X;}
	float getMinX(){return m_fmin_X;}
	float getMaxY(){return m_fmax_Y;}
	float getMinY(){return m_fmin_Y;}
};


#endif
